package com.eportal.gd;

import java.io.ByteArrayInputStream;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.eportal.core.Util;

public class transfer_request {
	public static String url = "POST /wangshangyh/banktrans/transfer_request.xml?usrID="
			+ Conf.gd_user
			+ "&userPassword="
			+ Conf.gd_pwd
			+ "&Sigdata=1 HTTP/1.0";

	private String batchID = "";
	private transferReq records[];

	public transfer_request(String batchID, transferReq records[]) {
		this.records = records;
		this.batchID = batchID;
	}

	public transfer unPack(String msg) throws Exception {
		System.out.println("unpack transfer...");
		transfer t = new transfer();
		SAXReader reader = new SAXReader();
		Document doc = null;
		doc = reader.read(new ByteArrayInputStream(msg.getBytes()));
		Element transfer = doc.getRootElement().element("transfer");
		Element transferRespond = transfer.element("transferRespond");
		Element transferRespondHeader = transferRespond
				.element("transferRespondHeader");
		String respondCode = transferRespondHeader.element("respondStatus")
				.elementText("respondCode");
		String respondInfo = transferRespondHeader.element("respondStatus")
				.elementText("respondInfo");
		if (respondCode.equals("0000") || respondCode.equals("0001")) {
			int transPatches = Integer.valueOf(transferRespondHeader
					.elementText("transPatches"));
			TransferRespondRecord[] records = new TransferRespondRecord[transPatches];
			Element transferRespondBody = transferRespond
					.element("transferRespondBody");
			int p = 0;
			for (Iterator i = transferRespondBody.elementIterator(); i
					.hasNext();) {
				Element record = (Element) i.next();
				TransferRespondRecord tr = new TransferRespondRecord();
				tr.setClientPatchID(record.elementText("clientPatchID"));
				tr.setReturnCode(record.elementText("returnCode"));
				tr.setReturnInfo(record.elementText("returnInfo"));
				tr.setTransDateTime(record.elementText("transDateTime"));
				records[p] = tr;
				p++;
			}
			t.setRecodes(records);
		}
		t.setRemark(respondInfo);
		t.setRespcode(respondCode);
		return t;
	}

	public String toString() {
		System.out.println("pack transfer...");
		int allsum = 0;
		Document document = DocumentHelper.createDocument();
		document.setXMLEncoding("GBK");
		Element root = document.addElement("transaction");
		Element element = root.addElement("transfer");
		element = element.addElement("transferRequest");
		Element hde = element.addElement("transferRequestHeader");
		hde.addElement("language").addText("zh-cn");
		hde.addElement("clientTime").addText(
				Util.getCurrentDate("yyyy-MM-dd|HH:mm:ss").replace("|", "T"));
		Element ue = hde.addElement("logonPart");
		ue.addElement("userID").addText("");
		ue.addElement("userPassword").addText("");
		ue.addElement("operatorID").addText("");
		hde.addElement("batchID").addText(batchID);
		hde.addElement("transPatches").addText(records.length + "");
		if (records.length > 10) {
			return null;
		}

		Element boyde = element.addElement("transferRequestBody");
		for (int i = 0; i < records.length; i++) {
			Element record = boyde.addElement("transferRequestRecord");
			record.addElement("clientPatchID").addText(
					batchID + Util.left_(i + "", "0", 4));
			String amt = records[i].getAmount();
			allsum += Integer.valueOf(amt);
			record.addElement("transferType").addText(
					records[i].getTransferType());
			record.addElement("accountNo").addText(records[i].getAccountNo());
			record.addElement("toAccountName").addText(
					records[i].getToAccountName());
			record.addElement("toAccountNo").addText(records[i].toAccountNo);
			record.addElement("toBank").addText(records[i].getToBank());
			record.addElement("amount").addText(Util.FormatSum(amt));
			record.addElement("toLocation").addText("");
			record.addElement("clientSignature").addText("");
			record.addElement("checkNo").addText("");
			record.addElement("checkPassword").addText("");
			record.addElement("note").addText(records[i].getNote());
		}
		hde.addElement("transSum").addText(Util.FormatSum("" + allsum));
		return document.asXML();
	}
}
